import numpy as np
import matplotlib.pyplot as plt
from sklearn.preprocessing import OneHotEncoder
import math

np.random.seed(1)

# onehot
m, n = 6, 4
y = np.floor(np.random.random(m) * (n + 1))
print('y')
print(y)

y_onehot = np.zeros([m, n])
for i in range(m):
    y_onehot[i, int(y[i])] = 1
print('y_onehot')
print(y_onehot)

b = OneHotEncoder(categories='auto')
y_onehot_lib = b.fit_transform(y.reshape(-1, 1)).toarray()  # Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.
print('y_onehot_lib')
print(y_onehot_lib)

print('unique: allclose(y_onehot, y_onehot_lib)', np.unique(np.allclose(y_onehot, y_onehot_lib)))

y_lib = b.inverse_transform(y_onehot_lib).ravel()
print('y_lib inverse')
print(y_lib)
print('unique: allclose(y, y_lib)', np.unique(np.allclose(y, y_lib)))
